//
//  ChangePlainViewController.m
//  Learn
//
//  Created by 迟建国 on 2024/4/1.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "ChangePlainViewController.h"

@interface ChangePlainViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>
@property (strong, nonatomic) UIPickerView *pickerView;
@property (strong, nonatomic) NSArray *firstColumnData;
@property (strong, nonatomic) NSArray *secondColumnData;
@property (assign, nonatomic) NSInteger selectedFirstComponentRow;
@property (assign, nonatomic) NSInteger selectedSecondComponentRow;
@property (strong, nonatomic) UIView *selectionView;
@end

@implementation ChangePlainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = rgbHex(0xF5F5F5, 1);
    [self setUI];
    [self setNavUI];
    [self setBottomUI];
    // Do any additional setup after loading the view.
}

- (void)setUI {
    
    UIImageView *topBgView = [[UIImageView alloc] init];
    topBgView.image = [UIImage imageNamed:@""];
    topBgView.backgroundColor = UIColor.grayColor;
    [self.view addSubview:topBgView];
    [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.offset(190 + NavigationBarHeight);
    }];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 15;
    bgView.layer.masksToBounds = YES;
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(10);
        make.top.equalTo(self.view.mas_top).offset( NavigationBarHeight + 20);
        make.right.equalTo(self.view.mas_right).offset(-10);
        make.height.offset(470);
    }];
    
    UIImageView *bookImageView = [[UIImageView alloc] init];
    bookImageView.image = [UIImage imageNamed:@""];
    bookImageView.backgroundColor = UIColor.redColor;
    [bgView addSubview:bookImageView];
    [bookImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView.mas_top).offset(15);
        make.left.equalTo(bgView.mas_left).offset(10);
        make.width.offset(68);
        make.height.offset(88);
    }];
    
    UILabel *nameL = [[UILabel alloc] init];
    nameL.text = @"姓名是谁说的是的";
    nameL.textColor = rgba(51, 51, 51, 1);
    nameL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    [bgView addSubview:nameL];
    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bookImageView.mas_top).offset(5);
        make.left.equalTo(bookImageView.mas_right).offset(15);
        make.right.equalTo(bgView.mas_right).offset(-10);
    }];
    
    UILabel *desL = [[UILabel alloc] init];
    desL.text = @"778个词";
    desL.textColor = rgba(142, 142, 142, 1);
    desL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:14];
    [bgView addSubview:desL];
    [desL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameL.mas_bottom).offset(10);
        make.left.equalTo(bookImageView.mas_right).offset(15);
        make.right.equalTo(bgView.mas_right).offset(-10);
    }];
    
    UILabel *statusL = [[UILabel alloc] init];
    statusL.text = @"学习中";
    statusL.textColor = [UIColor whiteColor];
    statusL.backgroundColor = rgba(244, 85, 85, 1);
    statusL.textAlignment = NSTextAlignmentCenter;
    statusL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:12];
    [bgView addSubview:statusL];
    [statusL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bookImageView.mas_bottom);
        make.left.equalTo(bookImageView.mas_right).offset(15);
        make.width.offset(58);
        make.height.offset(20);
    }];
    
    UIView *topLineView = [[UIView alloc] init];
    topLineView.backgroundColor = rgba(229, 229, 229, 1);
    [bgView addSubview:topLineView];
    [topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(bgView);
            make.top.equalTo(bgView.mas_top).offset(133);
            make.height.offset(1);
    }];
    
    UILabel *plainL = [[UILabel alloc] init];
    plainL.text = @"我计划完成天数/每天背单词个数";
    plainL.textColor = rgba(51, 51, 51, 1);
    plainL.textAlignment = NSTextAlignmentCenter;
    plainL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    [bgView addSubview:plainL];
    [plainL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topLineView.mas_bottom).offset(30);
        make.centerX.equalTo(bgView.mas_centerX);
    }];
    
    UIView *timeView = [[UIView alloc] init];
    timeView.backgroundColor = rgba(12, 183, 108, 0.1);
    timeView.layer.cornerRadius = 14;
    timeView.layer.masksToBounds = YES;
    [bgView addSubview:timeView];
    [timeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bgView.mas_centerX);
        make.top.equalTo(plainL.mas_bottom).offset(15);
        make.height.offset(28);
        make.width.offset(220);
    }];
    
    UIImageView *alarmImage = [[UIImageView alloc] init];
    alarmImage.image = [UIImage imageNamed:@"icon_alarm"];
    [timeView addSubview:alarmImage];
    [alarmImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(timeView.mas_centerY);
        make.left.equalTo(timeView.mas_left).offset(24);
        make.height.width.offset(12);
    }];
    UILabel *alarmL = [[UILabel alloc] init];
    alarmL.text = @"计划完成时间：2024-07-12";
    alarmL.textColor = rgba(12, 183, 108, 1);
    alarmL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:12];
    [timeView addSubview:alarmL];
    [alarmL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(timeView.mas_centerY);
        make.left.equalTo(alarmImage.mas_right).offset(5);
    }];
    
    // 初始化数据
    self.firstColumnData = @[@"58天", @"68天", @"78天",@"88天",@"98天",@"108天"];
    self.secondColumnData = @[@"10个", @"15个", @"20个", @"25个"];
    
    // 创建 UIPickerView
    self.pickerView = [[UIPickerView alloc] init];
    [self.pickerView setTintColor:UIColor.blueColor];
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
    [bgView addSubview:self.pickerView];
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(10);
        make.top.equalTo(timeView.mas_bottom).offset(25);
        make.right.equalTo(bgView.mas_right).offset(-10);
        make.height.offset(195);
    }];
    
    self.selectionView = [[UIView alloc] initWithFrame:CGRectMake(-1, 0, self.pickerView.bounds.size.width + 2, 42)];
    self.selectionView.backgroundColor = [UIColor clearColor];
    self.selectionView.layer.borderWidth = 1.0;
    self.selectionView.layer.borderColor = rgba(229, 229, 229, 1).CGColor; // 设置选中框的颜色
    [self.pickerView addSubview:self.selectionView];
    [self.selectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(-1);
        make.right.equalTo(bgView.mas_right).offset(-1);
        make.centerY.equalTo(self.pickerView.mas_centerY);
        make.height.offset(42);
    }];
    self.pickerView.tintColor = [UIColor blueColor];

}

- (void)setNavUI {
    UIView *nav = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, NavigationBarHeight)];
    nav.backgroundColor = rgba(41, 241, 156, 1);
    [self.view addSubview:nav];
//    self.nav = nav;
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"icon_back_white"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [nav addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nav.mas_left).offset(5);
        make.bottom.equalTo(nav.mas_bottom);
        make.width.offset(50);
        make.height.offset(44);
    }];
    
    UILabel *titleL = [[UILabel alloc] init];
    titleL.text = @"修改计划";
    titleL.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    titleL.textColor = UIColor.whiteColor;
    titleL.textAlignment = NSTextAlignmentCenter;
    [nav addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(nav.mas_bottom);
        make.centerX.equalTo(nav.mas_centerX);
        make.height.offset(44);
    }];
    
    UIButton *changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [changeBtn setTitle:@"切换教材" forState:UIControlStateNormal];
    [changeBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    changeBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:14];
    [changeBtn addTarget:self action:@selector(changeBook) forControlEvents:UIControlEventTouchUpInside];
    [nav addSubview:changeBtn];
    [changeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-10);
        make.bottom.equalTo(nav.mas_bottom);
        make.height.offset(44);
    }];
    
}

- (void)setBottomUI{
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_H - 55 - SafeAreaBottomHeight, SCREEN_W, 55)];
    bottomView.backgroundColor = rgbHex(0xF5F5F5, 1);
    [self.view addSubview:bottomView];
    
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    submitBtn.backgroundColor = rgba(41, 241, 156, 1);
    submitBtn.layer.cornerRadius = 22.5;
    submitBtn.layer.masksToBounds = YES;
    submitBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:18];
    [submitBtn setTitle:@"确定" forState:UIControlStateNormal];
    [submitBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [submitBtn addTarget:self action:@selector(submitBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:submitBtn];
    
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(10);
        make.right.equalTo(self.view.mas_right).offset(-10);
        make.bottom.equalTo(self.view.mas_bottom).offset(-SafeAreaBottomHeight - 10);
        make.height.offset(45);
    }];
    
}

#pragma mark - UIPickerViewDataSource Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2; //
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.firstColumnData.count;
    } else {
        return self.secondColumnData.count;
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *label = [[UILabel alloc] init];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = rgba(181, 181, 181, 1);
    label.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    
    if (component == 0) {
        label.text = self.firstColumnData[row];
    } else {
        label.text = self.secondColumnData[row];
    }
    pickerView.showsSelectionIndicator = NO;
    if (component == 0 && row == self.selectedFirstComponentRow) {
        label.textColor = rgba(12, 183, 108, 1); // 选中的颜色
    }
        
    if (component == 1 && row == self.selectedSecondComponentRow) {
        label.textColor = rgba(12, 183, 108, 1); // 选中的颜色
    }
    
    return label;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 42;
}

#pragma mark - UIPickerViewDelegate Methods

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        return self.firstColumnData[row];
    } else {
        return self.secondColumnData[row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
            self.selectedFirstComponentRow = row;
        } else {
            self.selectedSecondComponentRow = row;
        }
    [pickerView reloadComponent:component];
}

- (void)changeBook {
    
}

- (void)submitBtnClicked {
    [AlertView alertWith:self title:@"切换教材版本后，现有的计划将会被 新计划" content:@"" cancel:@"取消" sure:@"确定" Complection:^{
        
    }];
}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
